package com.fh.modules.dbms.controller;

import com.fh.common.api.vo.Result;
import com.fh.common.aspect.annotation.AutoLog;
import com.fh.common.concurrent.ListRecursiveAction;
import com.fh.common.constant.CommonConstant;
import com.fh.common.distributedlock.DistributeLock;
import com.fh.common.exception.Assert;
import com.fh.db.*;
import com.fh.modules.dbms.entity.DbmsView;
import com.fh.modules.dbms.service.IDbmsDbsourceService;
import com.fh.modules.dbms.service.IDbmsPackageService;
import com.fh.modules.dbms.service.IDbmsTableService;
import com.fh.modules.dbms.service.IDbmsViewService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author qiujunda
 * @Date 23/07/2020
 * @Description
 */
@RestController
@Api("数据库采集")
public class DbmsCollectController {
    @Autowired
    private IDbmsDbsourceService dbmsDbsourceService;
    @Autowired
    private IDbmsTableService dbmsTableService;
    @Autowired
    private IDbmsViewService dbmsViewService;
    @Autowired
    private IDbmsPackageService dbmsPackageService;
    @Autowired
    private DistributeLock distributeLock;

    @PostMapping(value = "/dbms/collect/collectView")
    @ApiOperation("采集视图")
    @AutoLog(value = "采集视图", logType = CommonConstant.LOG_TYPE_ACTION)
    public Result collectView(Long sourceId) throws Exception {
        boolean lock = distributeLock.lock("collect", 1800);
        Assert.isFalse(lock, "有任务正在采集，请稍后再试");
        try {
            DbSource dbSource = dbmsDbsourceService.getDbSource(sourceId.intValue());
            DbSchema dbSchema = dbSource.getSchema();
            List<DbView> list = dbSchema.getViews();
            String collectSid = String.valueOf(System.currentTimeMillis());
            ListRecursiveAction<DbView> action = ListRecursiveAction.buildListRecursiveAction(list, 0, list.size());
            action.setListTask(dbView -> {
                DbmsView dbmsView = new DbmsView();
                BeanUtils.copyProperties(dbView, dbmsView);
                dbmsView.setDelFlag(0);
                dbmsView.setSourceId(sourceId);
                dbmsView.setCollectSid(collectSid);
                dbmsViewService.addOrUpdateDbmsView(dbmsView);
            });
            action.run();
            dbmsViewService.checkAndDeleteDbmsView(sourceId, collectSid);
        } catch (Exception e) {
            throw e;
        } finally {
            distributeLock.unLock("collect");
        }
        return Result.ok("成功");
    }

    @PostMapping(value = "/dbms/collect/collectTable")
    @ApiOperation("采集表")
    @AutoLog(value = "采集表", logType = CommonConstant.LOG_TYPE_ACTION)
    public Result collectTable(Long sourceId) throws Exception {
        boolean lock = distributeLock.lock("collect", 1800);
        Assert.isFalse(lock, "有任务正在采集，请稍后再试");
        try {
            DbSource dbSource = dbmsDbsourceService.getDbSource(sourceId.intValue());
            DbSchema dbSchema = dbSource.getSchema();
            List<DbTable> dbTableList = dbSchema.getTables();
            String collectSid = String.valueOf(System.currentTimeMillis());
            ListRecursiveAction<DbTable> action = ListRecursiveAction.buildListRecursiveAction(dbTableList, 0, dbTableList.size());
            action.setListTask(dbTable -> {
                dbmsTableService.addOrUpdateDbmsTable(dbTable, sourceId, collectSid);
            });
            action.run();
        } catch (Exception e) {
            throw e;
        }finally {
            distributeLock.unLock("collect");
        }
        return Result.ok("成功");
    }

    @PostMapping(value = "/dbms/collect/collectPackage")
    @ApiOperation("采集包")
    @AutoLog(value = "采集包", logType = CommonConstant.LOG_TYPE_ACTION)
    public Result collectPackage(Long sourceId) throws Exception {
        boolean lock = distributeLock.lock("collect", 1800);
        Assert.isFalse(lock, "有任务正在采集，请稍后再试");
        try {
            DbSource dbSource = dbmsDbsourceService.getDbSource(sourceId.intValue());
            DbSchema dbSchema = dbSource.getSchema();
            List<DbPackage> dbPackageList = dbSchema.getPackages();
            String collectSid = String.valueOf(System.currentTimeMillis());
            ListRecursiveAction<DbPackage> action = ListRecursiveAction.buildListRecursiveAction(dbPackageList, 0, dbPackageList.size());
            action.setListTask(dbPackage -> {
                dbmsPackageService.addOrUpdateDbmsPackage(dbPackage, sourceId, collectSid);
            });
            action.run();
        } catch (Exception e) {
            throw e;
        }finally {
            distributeLock.unLock("collect");
        }
        return Result.ok("成功");
    }
}
